#!/usr/bin/perl

use strict;
use Data::Dumper;

my @str = qw/setfsrecoveryrequired clearfsrecoveryrequired startfsrecovery bringfsonline abortfsrecovery queryfsrecovery/;
print "str is : @str \n";
print "str array long is : ", $#str + 1 ,"\n";
print "str array lenght is : ", scalar(@str) , ' or @str + 0 :', @str + 0, "\n";
#str is : setfsrecoveryrequired clearfsrecoveryrequired startfsrecovery bringfsonline abortfsrecovery queryfsrecovery
#str array long is : 6
#str array lenght is : 6 or @str + 0 :6

# Hash
my %longday = (
    "Sun" => "Sunday",
    "Mon" => "Monday",
    "Tue" => "Tuesday",
    "Wed" => "Wednesday",
    "Thu" => "Thursday",
    "Fri" => "Friday",
    "Sat" => "Saturday"
);
my %wife=(); # Need to define first
$wife{"Jacob"} = ["Leah", "Rachel", "Bilhah", "Zilpah"];
print "Jacob's first wife is : $wife{Jacob}[0]","\n";

my %kids_of_wife = ();
$kids_of_wife{"Jacob"} = {
    "Leah"   => ["Reuben", "Simeon", "Levi", "Judah", "Issachar", "Zebulun"],
    "Rachel" => ["Joseph", "Benjamin"],
    "Bilhah" => ["Dan", "Naphtali"],
    "Zilpah" => ["Gad", "Asher"]
};
print $kids_of_wife{"Jacob"}{"Rachel"}[1],"\n";


my $e = exp(1);
my $aref = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
];
foreach my $item (@str) 
{
    if ($item =~ /\S*fsrecovery\S*/ || $item eq "bringfsonline")
    {
        print "Ok - $item\n";
    }
    else 
    {
        print "KO - $item\n";
    }
}
#print Dumper(%longday);

# reference
my $scalar = 1 ;
my @array = (1, 2, 3) ;
my $relarr = \@array;
my $arr2 = @{$relarr};
my @arr3 = [@{$relarr}];
print "==========================\n";
print Dumper($relarr);
print "----\n";
print Dumper($arr2);
print "----\n";
print Dumper(@arr3);
print "=============================\n";

my %hash = ('zdd' => 30, 'autumn' => 27) ;

my $sref = \$scalar ;   # scalar reference
my $aref = \@array ;    # array reference
my $href = \%hash ;     # hash reference

print $$sref, "\n" ;   # .$sref..sref
print Dumper($$sref);
print @$aref, "\n" ;   # .$aref..aref 
print Dumper(@$aref);
print %$href, "\n" ;   # .$href..href
print Dumper(%$href);
print $$aref[2], "\n" ;
print $$href{'zdd'}, "\n"; 


#while (defined($_ = <STDIN>)) {print $_; }   
#while ($_ = <STDIN>) { print; }         
#while (<STDIN>) { print ;}         
#for (;<STDIN>;) { print;}         
#print $_ while defined( $_ = <STDIN>);      
#print while $_ = <STDIN>;         
#print while <STDIN>;            


# from right to left
print "value is : ", 2 ** 3 ** 2, "\n";  # output: 512


sub readData {
    my ($mydata) = @_;
    print $mydata . "\n";

}


readData("aa", "bb", "cc", "dd");
sub strToInt($) {
    my ($str) = @_;

    if ($str =~ /^0[xX]/) {
        my $len = length($str);
        if ($len <= 10)
        {
            return hex($str);
        } else {
            my $high = hex(substr($str, 0, $len-8));
            my $low  = hex("0x".substr($str, $len-8, $len));
            my $data = $high << 32;
            $data += $low;
            return $data;
        }
    } elsif ($str =~ /^0[oO](\d*)/) {
        return oct $1;
    } else {
        return int($str);
    }
}

print strToInt(0x0000cdb30) . "\n";


my @input_numbers = sort qw/231 341 56 678 931/;
my @count = grep /1$/, @input_numbers;
my @results = grep $_ > 300, @input_numbers;
print @count . "\n";
print @results . "\n";

# Shift
sub digit_sum_is_odd {
    my $input = shift;
    print $input . "\n";
    my @digits = split //, $input;
    print @digits . "\n";
    my $sum;
    $sum += $_ for @digits;
    return $sum;
}
print digit_sum_is_odd("342332");
print "\n";

my %hash = (
    "beer" => "good",
    "bananas" => (
        "green"  => "wait",
        "yellow" => "eat",
    ),
);
print $hash{"bananas"} . "\n";
my $scalar = ("Alpha", "Beta", "Gamma", "Pie");
print $scalar . "\n";

my $colour    = "Indigo";
my $scalarRef = \$colour;
print $colour . "\n";         # "Indigo"
print $scalarRef . "\n";      # ..... "SCALAR(0x182c180)"
print ${ $scalarRef } . "\n"; # "Indigo"


my @lunsnap = [
    { owning_device_id => '206839508', properties => '', semaphores => 'HASH(0x11bc33d4)', shared_id => '248424908' },
    { owning_device_id => '206839508', properties => '', semaphores => 'HASH(0x11bb399c)', shared_id => '248425180' }
];
my @lunsnap2 = (
    { owning_device_id => '206839508', properties => '', semaphores => 'HASH(0x11bc33d4)', shared_id => '248424908' },
    { owning_device_id => '206839508', properties => '', semaphores => 'HASH(0x11bb399c)', shared_id => '248425180' }
);
print "Lun1: " .@lunsnap;
print "\n";
print "Lun2: " . @lunsnap2;
print "\n";

my @abc = (1,2,3);
my %abc = ("a" => "b", "c" => "d");
my $habc = \%abc;
$habc->{"name"} = [@abc];
print "\n";
#$habc->{"name"}->{0} = $abc[2];
#print $habc->{"name"}->{0};
print "\n";

my $aref = [1, [2, 3], [4, 5, 6]] ;
print $aref->[1] , "\n" ; 

my @array = (
    "apples",
    "bananas",
    (
        "inner",
        "list",
        "several",
        "entries",
    ),
    "cherries",
);

#print @array;
my %hash = (
    "beer" => "good",
    "bananas" => (
        "green"  => "wait",
        "yellow" => "eat",
    ),
);
foreach my $k (keys %hash) {
 print "hash{$k}=$hash{$k}.\n"
}
print %hash;

my %res = ("stdout" => "Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection

1:    ID                             = sv_4
      Name                           = ss
      Description                    =
      Group                          =
      Storage pool ID                = pool_3
      Storage pool                   = RecoveryTestPool
      Health state                   = OK (5)
      Health details                 = 'The LUN is operating normally. No action is required.'
      Size                           = 10737418240 (10.0G)
      Maximum size                   = 17592186044416 (16.0T)
      Thin provisioning enabled      = yes
      Deduplication enabled          =
      Inline compression enabled     = no
      Inline compression space saved = 0
      Compression Percent            = 0%
      Current allocation             = 1049329664 (1000.7M)
      Protection size used           = 0
      Snapshot count                 = 0
      Protection schedule            =
      Protection schedule paused     = no
      WWN                            = 60:06:01:60:15:E0:32:00:AA:35:0F:54:4F:1A:5B:0C
      Replication destination        = no
      Creation time                  = 2014-09-09 17:15:22
      Last modified time             = 2014-09-09 17:15:42
      SP owner                       = SPA
      Trespassed                     = no
      FAST VP policy                 = Start high then auto-tier
      FAST VP distribution           = Extreme Performance: 0%, Performance: 100%, Capacity: 0%
      LUN access hosts               =
      Snapshots access hosts         =
      IO limit                       =
      Effective maximum IOPS         = N/A
      Effective maximum KBPS         = N/A
");

print "\n";
print $res{"stdout"}. "\n";
if($res{"stdout"} =~ m/Current allocation\s*=\s*(\d+)/) {
    my $s = $1;
    my $dt = $s/123456788;
    print $dt."\n";
}

my $temp = join( $", @array ); print $temp. "\n";
my @colours = ("Red", "Orange", "Yellow", "Green", "Blue");
my $arrayRef = \@colours;
#print $arrayRef[0], "\n";
print $$arrayRef[0], "\n";

my $Price = "13";
print <<EOF;   
The price is $Price.
EOF

sub strToInt($) {
    my ($str) = @_;

    if ($str =~ /^0[xX]/) {
        my $len = length($str);
        if ($len <= 10)
        {
            # smaller than 0xffffffff
            return hex($str);
        } else {
            # perl 5.8.8 don't have 64bit hex()
            my $high = hex(substr($str, 0, $len-8));
            my $low  = hex("0x".substr($str, $len-8, $len));
            my $data = $high << 32;
            $data += $low;
            return $data;
        }
    } elsif ($str =~ /^0[oO](\d*)/) {
        return oct $1;
    } else {
        return int($str);
    }
}
my $vbm_address = "0x10000003984";
print "vbm_address($vbm_address)", strToInt($vbm_address);
print "\n";

my @datastr = ("aa","bb","cc", "aa", "dd", "ee", "aa", "cc", "ff", "dm");
my %hash = ();
$hash{$_}++ for @datastr;
#print Data::Dumper->Dump(%hash) . "\n";

my $dStr = "1474099090: UFS: 6:       9438   DenseFile1";
my $dStr =~ /.*(\d+)\s*(\S+)\s*/;
print $1, "--", $2, "\n";
